package com.mes.controller.manu;

import com.mes.entity.User;
import com.mes.service.manu.JobOrderService;
import com.yy.base.common.action.BaseCtrl;
import com.yy.base.page.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("jobOrderController")
public class JobOrderController extends BaseCtrl {
	@Autowired
	private JobOrderService service;

	//获取设备信息
	@ResponseBody
	@RequestMapping(value="sendOrders", method=RequestMethod.POST)
	public Message sendOrders(HttpServletRequest request) {
		Message msg = new Message();
		User user =  this.getLoginUser(request);

		String []ids = request.getParameter("ids").split(",");
		String colorId = request.getParameter("colorId");
		String ysids = request.getParameter("ysids");
		String productId = request.getParameter("productId");
		String WLids = request.getParameter("WLids");

		String id = service.savePaibannLog(ids, WLids, ysids, user.getOrgId(), user.getId());
		msg.setData("id", id);
		return msg;
	}

	//获取生产工单信息
	@ResponseBody
	@RequestMapping(value="getJobOrderList", method=RequestMethod.POST)
	public Message getJobOrderList(HttpServletRequest request) {
		Message msg = new Message();
		try {
			String code = request.getParameter("code");
			String colorId = request.getParameter("colorId");
			String product = request.getParameter("product");
			String ids = request.getParameter("ids");

			List<Map<String, Object>> data = service.getJobOrderList(colorId, product, code);
			if (data == null || data.isEmpty()) {
				msg.setErrorMessage("当前无生产信息！");
			} else {
				msg.setData("code", 0);
				msg.setData("data", data);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setErrorMessage("查询异常：" + e.getMessage());
		}
		return msg;
	}

	//获取设备信息
	@ResponseBody
	@RequestMapping(value="getSelectorList", method=RequestMethod.POST)
	public Message getSelectorList(HttpServletRequest request) {
		Message msg = new Message();
		try {
			msg.setData("products", service.getProductList());
			msg.setData("colors", service.getColorList());
		} catch (Exception e) {
			e.printStackTrace();
			msg.setErrorMessage("查询异常：" + e.getMessage());
		}
		return msg;
	}

	//获取排版列表
	@ResponseBody
	@RequestMapping(value="getJobPaibanInfo", method=RequestMethod.POST)
	public Message getJobPaibanInfo(HttpServletRequest request) {
		Message msg = new Message();
		try {
			String id = request.getParameter("id");

			List<Map<String, Object>> data = service.getJobPaibanInfo(id);
			if (data == null || data.isEmpty()) {
				msg.setErrorMessage("当前无生产信息！");
			} else {
				msg.setData("code", 0);
				msg.setData("data", data);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setErrorMessage("查询异常：" + e.getMessage());
		}
		return msg;
	}

	//获取排版历史记录
	@ResponseBody
	@RequestMapping(value="getJobPaibanList", method=RequestMethod.POST)
	public Message getJobPaibanList(HttpServletRequest request) {
		Message msg = new Message();
		try {
			String code = request.getParameter("code");
			String colorId = request.getParameter("colorId");
			String product = request.getParameter("product");
			String time = request.getParameter("time");

			List<Map<String, Object>> data = service.getJobPaibanList(code, product, colorId, time);
			if (data == null || data.isEmpty()) {
				msg.setErrorMessage("当前无生产信息！");
			} else {
				msg.setData("code", 0);
				msg.setData("data", data);
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setErrorMessage("查询异常：" + e.getMessage());
		}
		return msg;
	}
}